package com.kurumi.medical.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class FTPUtil {

	private static Logger logger = LoggerFactory.getLogger(FTPUtil.class);
    public FTPClient ftp;  
    public ArrayList<String> arFiles;  
      
   
      
    /** 
     * 登陆FTP服务器 
     * @param host FTPServer IP地址 
     * @param port FTPServer 端口 
     * @param username FTPServer 登陆用户名 
     * @param password FTPServer 登陆密码 
     * @return 是否登录成功 
     * @throws IOException 
     */  
    public boolean login(String host,int port,String username,String password) throws IOException{  
    	this.ftp.enterLocalPassiveMode();
        this.ftp.connect(host,port);  
        if(FTPReply.isPositiveCompletion(this.ftp.getReplyCode())){  
            if(this.ftp.login(username, password)){  
                this.ftp.setControlEncoding("GBK");  
                return true;  
            }  
        }  
        if(this.ftp.isConnected()){  
            this.ftp.disconnect();  
        }  
        return false;  
    }  
      
    /** 
     * 关闭数据链接 
     * @throws IOException 
     */  
    public void disConnection() throws IOException{  
        if(this.ftp.isConnected()){  
            this.ftp.disconnect();  
        }  
    }  
      
    /** 
     * 递归遍历出目录下面所有文件 
     * @param pathName 需要遍历的目录，必须以"/"开始和结束 
     * @throws IOException 
     */  
    public void List(String pathName) throws IOException{  
        if(pathName.startsWith("/")&&pathName.endsWith("/")){  
            String directory = pathName;  
            //更换目录到当前目录  
            this.ftp.changeWorkingDirectory(directory);  
            FTPFile[] files = this.ftp.listFiles();  
            for(FTPFile file:files){  
                if(file.isFile()){  
                    arFiles.add(directory+file.getName());  
                }else if(file.isDirectory()){  
                    List(directory+file.getName()+"/");  
                }  
            }  
        }  
    }  
      
    /** 
     * 递归遍历目录下面指定的文件名 
     * @param pathName 需要遍历的目录，必须以"/"开始和结束 
     * @param ext 文件的扩展名 
     * @throws IOException  
     */  
    public void List(String pathName,String ext) throws IOException{  
        if(pathName.startsWith("/")&&pathName.endsWith("/")){  
            String directory = pathName;  
            //更换目录到当前目录  
            this.ftp.changeWorkingDirectory(directory);  
            FTPFile[] files = this.ftp.listFiles();  
            for(FTPFile file:files){  
                if(file.isFile()){  
                    if(file.getName().endsWith(ext)){  
                        arFiles.add(directory+file.getName());  
                    }  
                }else if(file.isDirectory()){  
                    List(directory+file.getName()+"/",ext);  
                }  
            }  
        }  
    } 
    
    
    public  String copyFTPFileList() throws IOException{
    	if(!login("132.147.0.167",21,"meddochtml","meddochtml")){
    		return "fail";
    	}
    	//更换目录到当前目录  
        this.ftp.changeWorkingDirectory("/MEDDOC/00/00/00/00/A1/IP_7/");  
    	 FTPFile[] files = this.ftp.listFiles();  
         for(FTPFile file:files){  
             if(file.isFile()){  
                System.out.println(file.getName());
             }
         } 
         
         return "success";
    }
    
//    public static void main(String[] args) throws IOException {
//		
//    	FTPUtil fTPUtil = new FTPUtil();
//    	fTPUtil.copyFTPFileList();
//	}
   
}
